<template>
	<div class="home">
		<div class="main_add">
			<div class="left">
				<p>欢迎使用</p>
				<div class="img"></div>
				<p>智学无忧商城后台登录</p>
			</div>
			<div class="right">
				<div class="right_dl">登录</div>
				<el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px"
					class="demo-ruleForm">
					<el-form-item prop="pass">
						<i class="el-icon-user" style="margin-right: 20px;"></i>
						<el-input style="width: 300px; border:0px;" type="text" placeholder="请输入用户名"
							v-model="ruleForm.pass" autocomplete="off"></el-input>
					</el-form-item>
					<el-form-item prop="checkPass">
						<i class="el-icon-key" style="margin-right: 20px;"></i>
						<el-input style="width: 300px; " type="password" placeholder="请输入密码"
							v-model="ruleForm.checkPass" autocomplete="off"></el-input>
					</el-form-item>
					<el-form-item prop="age">
						<i class="el-icon-position" style="margin-right: 20px;"></i>
						<el-input style="width: 250px; " placeholder="666666" v-model.number="ruleForm.age"></el-input>
						<a style="color: #666666; margin-left: 10px;">验证码</a>
					</el-form-item>
					<el-form-item>
						<el-checkbox v-model="checked" @change="handleCheckedCitiesChange">记住密码</el-checkbox>
					</el-form-item>
					<el-form-item style="margin-right: 30px;">
						<el-button style="width: 340px; text-align: center;" type="primary"
							@click="submitForm('ruleForm')">登录</el-button>
					</el-form-item>
				</el-form>
				<div style="color: #999999; text-align: center; font-size: 14px; margin-top: 60px;">开发者:xxx</div>
			</div>
		</div>
	</div>
</template>

<script>
	export default {
		data() {
			var validatePass = (rule, value, callback) => {
				if (value === '') {
					callback(new Error('输入内容不能为空'));
				} else {
					if (this.ruleForm.checkPass !== '') {
						this.$refs.ruleForm.validateField('checkPass');
					}
					callback();
				}
			};
			var validatePass2 = (rule, value, callback) => {
				if (value === '') {
					callback(new Error('请再次输入密码'));
				} else {
					callback();
				}
			};
			return {
				checked: false,
				ruleForm: {
					pass: '',
					checkPass: '',
					age: 666666
				},
				rules: {
					pass: [{
						validator: validatePass,
						trigger: 'blur'
					}],
					checkPass: [{
						validator: validatePass2,
						trigger: 'blur'
					}]
				}
			};
		},
		created() {
			if (sessionStorage.getItem('password') == Number(123456)) {
				this.ruleForm.pass = sessionStorage.getItem('uname')
				this.ruleForm.checkPass = sessionStorage.getItem('password')
			} else {
				this.ruleForm.pass = ''
				this.ruleForm.checkPass = ''
			}
		},
		methods: {
			handleCheckedCitiesChange(e) {
				this.checked = e
			},
			submitForm(formName) {
				this.$refs[formName].validate((valid) => {
					if (valid) {
						this.axios.get(
								`/admin?_gp=admin&_mt=login&username=${this.ruleForm.pass}&password=${this.ruleForm.checkPass}&verifyCode=${this.ruleForm.age} `
							)
							.then((response) => {
								console.log(this.response)
								if (response.data.errmsg == "成功") {
									this.$message({
										showClose: true,
										message: '登录成功',
										type: 'success'
									});
									this.$router.replace("/About")
									sessionStorage.setItem('token', response.data.data)
									if (this.checked) {
										sessionStorage.setItem('uname', this.ruleForm.pass)
										sessionStorage.setItem('password', this.ruleForm.checkPass)
									} else {
										sessionStorage.removeItem('uname')
										sessionStorage.removeItem('password')
									}
								} else if (response.data.errmsg == "密码错误") {
									this.$message({
										showClose: true,
										message: '密码错误',
										type: 'error'
									});
								}
							})
					} else {
						console.log('error submit!!');
						return false;
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped="scoped">
	.home {
		width: 100%;
		height: 100vh;
		background: url(../assets/cloud.bef3ccbc.jpg);
		background-size: 100% 100%;
	}

	.main_add {
		width: 1000px;
		height: 500px;
		border-radius: 5px;
		position: absolute;
		top: 20%;
		left: 20%;
		box-shadow: -4px 5px 10px rgb(0 0 0 / 40%);
	}

	.left {
		width: 500px;
		height: 500px;
		float: left;
		border-radius: 5px 0px 0px 5px;
		background-color: #409EFF;
	}

	.left>p:nth-child(1) {
		color: #FFFFFF;
		font-size: 20px;
		text-indent: 2em;
		margin-bottom: 100px;
	}

	.img {
		background: url(../assets/zxwy.44bfa70d.jpg);
		background-size: 100px 100px;
		width: 100px;
		height: 100px;
		margin: auto;
	}

	.left>p:nth-child(3) {
		color: #FFFFFF;
		font-size: 30px;
		text-align: center;
		padding-top: 50px;
	}

	.right {
		width: 500px;
		height: 500px;
		float: right;
		border-radius: 0px 5px 5px 0px;
		background-color: #FFFFFF;
	}

	.right_dl {
		font-size: 25px;
		line-height: 120px;
		text-align: center;
		margin: auto;
	}
</style>
